﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RH.USER.BLL.Service;
using RH.USER.DAL.Repository;
using RH.USER.MODEL.Models;

namespace RH.USER.WEB.Controllers
{


    [Route("api/[controller]")]
    [ApiController]
    public class UserController : Controller
    {
        //依赖注入
        private readonly IUserService _userService;
 
       
        private readonly ILogger<UserController> _logger = null;
        private ILoggerFactory _Factory = null;



        public UserController(IUserService userService, ILogger<UserController> logger, ILoggerFactory factory)
        {

            _userService = userService;
            _logger = logger;
            _Factory = factory;
        }

        public IActionResult Index()
        {

            //Response.Headers.Add("Access-Control-Allow-Origin", "*");
            //Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
            //Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Authorization");

            return View("调用");
        }



        [HttpPost("GetLoginUser")]
        public async Task<IActionResult> GetLoginUser([FromBody] User user)
        {
            try
            {
                if (user == null || string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.UserPassword))
                {
                    return BadRequest("Username and password are required");
                }

                var isAuthenticated = await _userService.GetAsyncUserInfo(user.UserName, user.UserPassword);
                if (isAuthenticated)
                {
                    _logger.LogInformation($"接收到的前端正常 登录user 数据: {JsonConvert.SerializeObject(user)}");
                    return Ok("Login successful");
                }
                else
                {
                    _logger.LogInformation($"接收到的前端异常 登录user 数据: {JsonConvert.SerializeObject(user)}");
                    return Unauthorized("Invalid username or password");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "处理 GetLoginUser 请求时发生错误");
                return StatusCode(500, "服务器内部错误");
            }
        }

        [HttpPost("GetUserAdd")]
        public async Task<IActionResult> GetUserAdd([FromBody] User user)
        {
            try
            {
                await _userService.AddUser(user);
                _logger.LogInformation($"接收到的前端 注册user 数据: {JsonConvert.SerializeObject(user)}");
                return Ok();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "处理 GetUserAdd 请求时发生错误");
                return StatusCode(500, "服务器内部错误");
            }
        }

        [HttpPost("GetUserPwd")]
        public async Task<IActionResult> GetUserPwd(string UserName)
        {

            try
            {
                if (string.IsNullOrEmpty(UserName))
                {
                    return BadRequest("用户名不能为空");
                }

                var password = await _userService.GetUserPasswordAsync(UserName);

                if (password == null)
                {
                    return NotFound("用户未找到");
                }
                return Ok(new { Password = password });
            }
            catch (Exception ex)
            {

                _logger.LogError(ex, "处理 GetUserPwd 请求时发生错误");
                return StatusCode(500, "服务器内部错误");
            }  

        }
    }
}
